/**
 * 
 */
package com.wubo.common.web;

import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;

import com.wubo.common.io.ProxyOutputStream;

/**
 * @author WuBo
 * @CreateDate 2012-2-17
 */
public class BaseAction {
	protected Log logger = LogFactory.getLog(this.getClass());
	
	protected static String SUCCESS = "success";
	
	public static void json(Object json){
		try{
			JsonUtils.write(JsonUtils.toString(json), ServletActionContext.getResponse().getOutputStream());
		}catch(Exception e){
			throw new RuntimeException(e);
		}
	}
	
	public static void jsonSuccess(String json, boolean isObject){
		try{
			String jsonString = null;
			if(json == null){
				jsonString = "{\"success\":true}";
			}else{
				jsonString = "{\"success\":true,\"data\":"+json+"}";
			}
			JsonUtils.write(jsonString, ServletActionContext.getResponse().getOutputStream());
		}catch(Exception e){
			throw new RuntimeException(e);
		}
	}
	
	public static void jsonSuccess(Object json){
		try{
			JsonUtils.write(getJsonSuccess(json), ServletActionContext.getResponse().getOutputStream());
		}catch(Exception e){
			throw new RuntimeException(e);
		}
	}
	
	public static void jsonIFrameSuccess(Object json){
		try{
			JsonUtils.write(getJsonSuccess(json), true, ServletActionContext.getResponse().getOutputStream());
		}catch(Exception e){
			throw new RuntimeException(e);
		}
	}
	
	public static void jsonError(Object json){
		try{
			JsonUtils.write(getJsonError(json, false), ServletActionContext.getResponse().getOutputStream());
		}catch(Exception e){
			throw new RuntimeException(e);
		}
	}
	
	public static void jsonError(Object json, String error){
		try{
			JsonUtils.write(getJsonError(json, error), ServletActionContext.getResponse().getOutputStream());
		}catch(Exception e){
			throw new RuntimeException(e);
		}
	}
	
	public static void jsonError(Object json, boolean refresh){
		try{
			JsonUtils.write(getJsonError(json, refresh), ServletActionContext.getResponse().getOutputStream());
		}catch(Exception e){
			throw new RuntimeException(e);
		}
	}
	
	public static void jsonIFrameError(Object json){
		try{
			JsonUtils.write(getJsonError(json, false), true, ServletActionContext.getResponse().getOutputStream());
		}catch(Exception e){
			throw new RuntimeException(e);
		}
	}
	
	private static String getJsonSuccess(Object json){
		if(json == null){
			return "{\"success\":true}";
		}else{
			try{
				return "{\"success\":true,\"data\":"+JsonUtils.toString(json)+"}";
			}catch(Exception e){
				throw new RuntimeException("序列化对象【"+json+"】时出错", e);
			}
		}
	}
	
	private static String getJsonError(Object json, boolean refresh) {
		if(json == null){
			return "{\"success\":false, \"refresh\":"+refresh+"}";
		}else{
			try{
				return "{\"success\":false,\"data\":"+JsonUtils.toString(json)+",\"refresh\":"+refresh+"}";
			}catch(Exception e){
				throw new RuntimeException("序列化对象【"+json+"】时出错", e);
			}
		}
	}
	
	private static String getJsonError(Object json, String error) {
		if(json == null){
			return "{\"success\":false, \"error\":\""+error+"\"}";
		}else{
			try{
				return "{\"success\":false,\"data\":"+JsonUtils.toString(json)+",\"error\":\""+error+"\"}";
			}catch(Exception e){
				throw new RuntimeException("序列化对象【"+json+"】时出错", e);
			}
		}
	}
	
	protected HttpServletRequest getHttpServletRequest(){
		return ServletActionContext.getRequest();
	}
	
	protected HttpServletResponse getHttpServletResponse(){
		return ServletActionContext.getResponse();
	}
	
	protected OutputStream proxyOutputStream(boolean allowClose, boolean allowFlush) throws IOException {
		return ProxyOutputStream.createInstance(ServletActionContext.getResponse().getOutputStream(),
				allowClose, allowFlush);
	}
	protected void close(OutputStream os) throws IOException {
		if(os instanceof ProxyOutputStream){
			((ProxyOutputStream)os).realClose();
		}else{
			os.close();
		}
	}
	protected void flush(OutputStream os) throws IOException {
		if(os instanceof ProxyOutputStream){
			((ProxyOutputStream)os).realFlush();
		}else{
			os.flush();
		}
	}
}
